using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Ninja.WebSockets;
using NLog.Extensions.Logging;

namespace Drive.LiveExcelHost
{
    public class Worker : BackgroundService
    {
        private readonly ILogger<Worker> _logger;
        static ILoggerFactory _loggerFactory;
        static IWebSocketServerFactory _webSocketServerFactory;

        public Worker(ILogger<Worker> logger)
        {
            _loggerFactory = new NLogLoggerFactory();
            _logger = logger;
            _webSocketServerFactory = new WebSocketServerFactory();
        }

        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            _logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now);
            await Task.Delay(1000, stoppingToken);
            int port = 9000;
            IList<string> supportedSubProtocols = new string[] { "chatV1", "chatV2", "chatV3" };
            WebSocketServer server = new WebSocketServer(_webSocketServerFactory, _loggerFactory, supportedSubProtocols);
            await server.StartAsync(port, stoppingToken);
        }
    }
}
